home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / AppsToGo / DTS.Lib / System6Help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  6.7 KB  |  256 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        System6Help.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "DTS.Lib2.h"
  26. #include "DTS.Lib.protos.h"
  27.  
  28. #ifndef __BALLOONS__
  29. #include <Balloons.h>
  30. #endif
  31.  
  32. #ifndef __RESOURCES__
  33. #include <Resources.h>
  34. #endif
  35.  
  36. extern HMMessageRecord    gHelpMessage;
  37. extern Rect                gHelpMessageRct;
  38. extern ControlHandle    gHelpMessageCtl;
  39. extern WindowPtr        gHelpWithWindow;
  40.  
  41. static PicHandle        gHelp6Pic;
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. static OSErr    HelpImageDocument(FileRecHndl frHndl);
  50. static OSErr    HelpInitContent(FileRecHndl frHndl, WindowPtr window);
  51. static OSErr    HelpFreeDocument(FileRecHndl frHndl);
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56. /*****************************************************************************/
  57.  
  58. #ifdef applec
  59. #pragma segment ATGSys6Help
  60. #endif
  61.  
  62. /*****************************************************************************/
  63. /*****************************************************************************/
  64.  
  65.  
  66.  
  67. OSErr    HelpInitDocument(FileRecHndl frHndl)
  68. {
  69.     FileRecPtr    frPtr;
  70.  
  71.     frPtr = *frHndl;
  72.     frPtr->fileState.calcFrameRgnProc        = nil;
  73.     frPtr->fileState.contentClickProc        = nil;
  74.     frPtr->fileState.contentKeyProc          = nil;
  75.     frPtr->fileState.drawFrameProc           = nil;
  76.     frPtr->fileState.freeDocumentProc        = HelpFreeDocument;
  77.     frPtr->fileState.freeWindowProc          = nil;
  78.     frPtr->fileState.imageProc               = HelpImageDocument;
  79.     frPtr->fileState.initContentProc         = HelpInitContent;
  80.     frPtr->fileState.readDocumentProc        = nil;
  81.     frPtr->fileState.readDocumentHeaderProc  = nil;
  82.     frPtr->fileState.resizeContentProc       = nil;
  83.     frPtr->fileState.scrollFrameProc         = nil;
  84.     frPtr->fileState.undoFixupProc           = nil;
  85.     frPtr->fileState.windowCursorProc        = nil;
  86.     frPtr->fileState.writeDocumentProc       = nil;
  87.     frPtr->fileState.writeDocumentHeaderProc = nil;
  88.  
  89.     return(noErr);
  90. }
  91.  
  92.  
  93.  
  94. /*****************************************************************************/
  95. /*****************************************************************************/
  96.  
  97.  
  98.  
  99. static OSErr    HelpImageDocument(FileRecHndl frHndl)
  100. {
  101. #ifndef __MWERKS__
  102. #pragma unused (frHndl)
  103. #endif
  104.  
  105.     DoDrawControls((*frHndl)->fileState.window, false);
  106.     return(noErr);
  107. }
  108.  
  109.  
  110.  
  111. /*****************************************************************************/
  112.  
  113.  
  114.  
  115. static OSErr    HelpInitContent(FileRecHndl frHndl, WindowPtr window)
  116. {
  117. #ifndef __MWERKS__
  118. #pragma unused (frHndl)
  119. #endif
  120.  
  121.     WindowPtr        oldPort;
  122.     OSErr            err;
  123.     Rect            scnRct, strRct, cntRct, ctlRct, rct, **rh;
  124.     short            xx, yy, cc;
  125.     Handle            txt;
  126.     StringHandle    sh;
  127.     StScrpHandle    stl;
  128.     ControlHandle    ctl;
  129.     TEHandle        te;
  130.     Str255            pstr;
  131.  
  132.     err = AddControlSet(window, '6hlp', kwStandardVis, 0, 0, nil);
  133.     if (err) return(err);
  134.  
  135.     scnRct = GetWindowDeviceRectNMB(gHelpWithWindow);        /* Rect of screen that window-with-help is most on. */
  136.  
  137.     if (!(ctl = CTENext(window, &te, nil, 1, false))) return(memFullErr);
  138.  
  139.     GetPort(&oldPort);
  140.     SetPort(gHelpWithWindow);
  141.     ctlRct = (*gHelpMessageCtl)->contrlRect;
  142.     LocalToGlobalRect(&ctlRct);                                /* Rect of control that is getting help. */
  143.     SetPort(oldPort);
  144.  
  145.     if (gHelp6Pic) KillPicture(gHelp6Pic);
  146.     gHelp6Pic = nil;
  147.  
  148.     pstr[0] = 0;
  149.     xx = yy = 0;
  150.  
  151.     switch (gHelpMessage.hmmHelpType) {
  152.         case khmmString:
  153.             pcpy(pstr, (StringPtr)gHelpMessage.u.hmmString);
  154.             break;
  155.         case khmmPict:
  156.             if (!(gHelp6Pic = (PicHandle)Get1Resource('PICT', gHelpMessage.u.hmmTERes))) return(ResError());
  157.             ReleaseResource((Handle)gHelp6Pic);
  158.             xx = (*gHelp6Pic)->picFrame.right  - (*gHelp6Pic)->picFrame.left;
  159.             yy = (*gHelp6Pic)->picFrame.bottom - (*gHelp6Pic)->picFrame.top;
  160.             break;
  161.         case khmmStringRes:
  162.             GetIndString(pstr, gHelpMessage.u.hmmStringRes.hmmResID, gHelpMessage.u.hmmStringRes.hmmIndex);
  163.             break;
  164.         case khmmTERes:
  165.             if (!(txt = Get1Resource('TEXT', gHelpMessage.u.hmmTERes))) return(memFullErr);
  166.             DetachResource(txt);
  167.             stl = (StScrpHandle)Get1Resource('styl', gHelpMessage.u.hmmTERes);
  168.             if (stl) DetachResource((Handle)stl);
  169.             UseControlStyle(ctl);
  170.             DisposeHandle(CTESwapText(te, txt, stl, false));
  171.             if (stl) DisposeHandle((Handle)stl);
  172.             rh = (Rect **)Get1Resource('RECT', gHelpMessage.u.hmmTERes);
  173.             if (rh) xx = (*rh)->right - (*rh)->left;
  174.             UseControlStyle(nil);
  175.             break;
  176.         case khmmSTRRes:
  177.             sh = (StringHandle)GetResource('STR ', gHelpMessage.u.hmmSTRRes);
  178.             if (sh) pcpy(pstr, *sh);
  179.             break;
  180.     }
  181.  
  182.     if (pstr[0]) {
  183.         UseControlStyle(ctl);
  184.         CTEPutPStr(ctl, pstr);
  185.         UseControlStyle(nil);
  186.     }
  187.  
  188.     SetPort(window);
  189.     if (!yy) {        /* If handling text... */
  190.         if (!xx) {
  191.             for (;;) {
  192.                 yy = CTEDocHeight(te);
  193.                 xx = (*ctl)->contrlRect.right - (*ctl)->contrlRect.left;
  194.  
  195.                 if (xx > scnRct.right - scnRct.left - 64) {
  196.                     xx = scnRct.right - scnRct.left - 64;
  197.                     break;
  198.                 }
  199.  
  200.                 if (xx > 4 * yy) break;
  201.  
  202.                 CTESize(te, (xx + xx / 2), yy, true);
  203.             }
  204.         }
  205.         yy = CTEDocHeight(te);
  206.         CTESize(te, xx, yy, true);
  207.         yy = CTEDocHeight(te);
  208.     }
  209.     SetPort(oldPort);
  210.  
  211.     MoveWindow(window, 16384, 16384, false);
  212.     ShowHide(window, true);
  213.     CleanSendBehind(window, (WindowPtr)-1);
  214.  
  215.     rct = (*ctl)->contrlRect;
  216.     SizeWindow(window, xx + rct.left, yy + 2 * rct.top, false);
  217.  
  218.     strRct = GetWindowStructureRect(window);        /* Structure rect of window-with-help. */
  219.     cntRct = GetWindowContentRect(window);            /* Content rect of window-with-help. */
  220.  
  221.     cc = (ctlRct.right + ctlRct.left) / 2;
  222.     xx = cc - xx / 2;
  223.     if (xx < (scnRct.left + 10)) xx = (scnRct.left + 10);
  224.     if (xx > scnRct.right - (cntRct.right - cntRct.left) - 10)
  225.         xx = scnRct.right - (cntRct.right - cntRct.left) - 10;
  226.  
  227.     yy = ctlRct.top - (strRct.bottom - strRct.top) - 10;
  228.     if (yy < scnRct.top + 2) {
  229.         yy = ctlRct.bottom + 10;
  230.         if (yy > scnRct.bottom - (strRct.bottom - strRct.top) - 2)
  231.             yy = scnRct.bottom - (strRct.bottom - strRct.top) - 2;
  232.     }
  233.     yy += (cntRct.top - strRct.top);
  234.  
  235.     MoveWindow(window, xx, yy, false);
  236.  
  237.     return(noErr);
  238. }
  239.  
  240.  
  241.  
  242. /*****************************************************************************/
  243.  
  244.  
  245.  
  246. static OSErr    HelpFreeDocument(FileRecHndl frHndl)
  247. {
  248.     if (gHelp6Pic) KillPicture(gHelp6Pic);
  249.     gHelp6Pic = nil;
  250.  
  251.     return(DefaultFreeDocument(frHndl));
  252. }
  253.  
  254.  
  255.  
  256.